package com.et.u0

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.RecyclerView
import com.et.core.goods.GoodsInfo
import com.et.core.goods.GoodsProvider
import com.et.core.util.Adapter
import com.et.u0.databinding.ItemMainActivityBinding

class MainModel : ViewModel() {

    private var call: (binding: ItemMainActivityBinding, t: GoodsInfo) -> Unit = { v, p ->
        v.label.text = p.name
    }

    private var vh: (inflater: LayoutInflater, parent: ViewGroup) -> ItemMainActivityBinding =
        { inflater, parent ->
            ItemMainActivityBinding.inflate(inflater, parent, false)
        }

    private val adapter: Adapter<GoodsInfo, ItemMainActivityBinding> by lazy {
        Adapter(call, vh)
    }

    fun bindGoodsAdapter(
        recyclerview: RecyclerView,
    ) {
        recyclerview.adapter = adapter
    }

    fun initGoodsHelper(context: Context) {
        GoodsProvider.getInstance().apply {
            registerContentObserver(context)
            observer { r, dest -> adapter.dispatchUpdate(r, dest) }
        }
    }

}